#!/usr/bin/env python
# -*- coding: UTF-8 -*-

'''
created by jj.wang17
2016-11-01

This script contains some tools used for azkaban


'''

import os
import re
import sys
import subprocess
from  az_util import *
import datetime
reload(sys)
sys.setdefaultencoding('utf-8')


def prepareAzkabanEnv():
    propertys = paraseAZPropertyFile()
    username = 'hiveAccess'
    cmd_add_linux_user = "sudo useradd  -g azkaban -s /sbin/nologin -M  %s"%(username)
    add_user_to_group = "sudo usermod -a -G azkaban %s"%(username)
    execcmd(username, cmd_add_linux_user)
    execcmd(username, add_user_to_group)
    return username




def get_argv(args):
    if len(args) >= 2:
        return True
    else:
        print " azkaban tools:, Please input cmd str (ex: 'bash example.sh'), then re-run the script."
        return False

def azkaban_main(cmd):
    username = prepareAzkabanEnv()
    cmd_final = "sudo -u %s sh -c '%s' "%(username, cmd)
    print cmd_final
    return_code =execcmd(username,cmd_final)
    print "exec cmd: %s, return code: %d"%(cmd_final, return_code)
    sys.exit(return_code)


if __name__ == '__main__':
    if not get_argv(sys.argv):
        sys.exit(1)
    cmd = sys.argv[1]
    azkaban_main(cmd)


